package github.ziwukun.jsonql.orm;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import github.ziwukun.jsonql.utils.CollectionUtils;

/**
 * 容器实现类
 * @author Hugo.Yu
 */
class ContainerImpl implements Container{
	String ctnName;
	ContainerType ctnType;
	IdFieldImpl id;
	List<CtnFieldImpl> fields = new LinkedList<>();
	
	ContainerImpl(ModelBuilder modelBuilder){
		ModelBuilder.instanceCtn(modelBuilder,this);
	}
	
	ContainerImpl(RltPropBuilder rltPropBuilder){
		RltPropBuilder.instanceRlfCtn(rltPropBuilder,this);
	}
	
	
	@Override
	public String ctnName() {
		return ctnName;
	}

	@Override
	public ContainerType ctnType() {
		return ctnType;
	}

	@Override
	public IdField id() {
		return id;
	}
	
	@Override
	public List<CtnField> fields() {
		return new ArrayList<>(fields);
	}
	
	@Override
	public boolean isFieldEmpty() {
		return fields.isEmpty();
	}

	@Override
	public CtnField getFieldByName(String fieldName) {
		if(StrUtil.isBlank(fieldName)) {return null;}
		
		return CollectionUtils.findOne(fields, field -> {
			return field.fieldName.equals(fieldName);
		});
	}

}
